package com.ruoyi.common.filter;


import com.ruoyi.common.utils.StringUtils;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//@ConditionalOnProperty(prefix = "security.csrf", name = "enable", havingValue = "true")
//@Configuration
//@WebFilter(filterName = "refererFilter", urlPatterns = "/*")

public class   RefererFilter {
    //public class   RefererFilter implements Filter{
//    public static final Logger logger = LoggerFactory.getLogger(RefererFilter.class);
//
//    /**
//     * 过滤器配置对象
//     */
//    FilterConfig filterConfig = null;
//
//    /**
//     * 是否启用
//     */
//    @Value("${security.csrf.enable:false}")
//    private boolean enable = true;
//    @Value("${RcfUrl.params}")
//    private String urlParams;
//    /**
//     * 忽略的URL
//     */
//    @Value("${security.csrf.excludes:}")
//    private String excludes;
//
//    @Override
//    public void init(FilterConfig filterConfig) throws ServletException {
//        this.filterConfig = filterConfig;
//    }
//
//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
//            throws IOException, ServletException {
//
//
////        HttpServletRequest request = (HttpServletRequest) servletRequest;
////        // 不启用或者已忽略的URL不拦截
////
////
////        String referer = request.getHeader("Referer");
////        String serverName = request.getServerName();
////        //服务器:22.40.209.39 当前域名http://22.40.209.115:18890/login?redirect=%2Findex
////        // 判断是否存在外链请求本站
////
////        List<String> list = Arrays.asList(urlParams.split(","));
////        //ip 集合
////        boolean isContext = false;
////        if(!"".equals(referer) && referer != null){
////            for(String a : list){
////                if(referer.contains(a)){
////                    isContext = true;
////                    break;
////                }
////            }
////        }
////
////
////        if(isContext){
////            filterChain.doFilter(servletRequest, servletResponse);
////            return;
////        }
////        if (referer == null || !referer.contains(serverName)) {
////            logger.error("Referer过滤器 服务器：{} 当前域名：{}", serverName, referer);
////            HttpServletResponse resp = (HttpServletResponse) servletResponse;
////            resp.setStatus(HttpStatus.NOT_FOUND.value());
////            return;
////        } else {
////            filterChain.doFilter(servletRequest, servletResponse);
////        }
//        filterChain.doFilter(servletRequest, servletResponse);
//        return;
//    }
//
//    @Override
//    public void destroy() {
//        this.filterConfig = null;
//    }
//
//
//
//    public static Logger getLogger() {
//        return logger;
//    }
//
//    public FilterConfig getFilterConfig() {
//        return filterConfig;
//    }
//
//    public void setFilterConfig(FilterConfig filterConfig) {
//        this.filterConfig = filterConfig;
//    }
//
//    public boolean isEnable() {
//        return enable;
//    }
//
//    public void setEnable(boolean enable) {
//        this.enable = enable;
//    }
//
//    public String getExcludes() {
//        return excludes;
//    }
//
//    public void setExcludes(String excludes) {
//        this.excludes = excludes;
//    }
}
